﻿using BookManege.service;

namespace BookManege.controller
{
    public partial class borrowBookInfo : Form
    {
        private bookBorrowService bookBorrowService;
        private long userId;
        private long bookId;
        private int number;
        private string bookName;
        private string auther;
        public borrowBookInfo(long userId, long bookId, int number, string bookName, string auther)
        {
            InitializeComponent();
            this.bookBorrowService = new bookBorrowService();
            this.userId = userId;
            this.bookId = bookId;
            this.number = number;
            this.bookName = bookName;
            this.auther = auther;
            numericUpDown1.Maximum = number;
        }

        private void button1_Click(object sender, EventArgs e)
        {
            if (bookBorrowService.borrowBook(userId, bookId, (int)numericUpDown1.Value, DateTime.Now, DateTime.Parse(dateTimePicker1.Value.ToString()).ToString("yyyy/MM/dd")) > 0)
            {
                MessageBox.Show("添加成功");
                Close();
            }
            else
            {
                MessageBox.Show("添加失败");
            }
        }

        private void borrowBookInfo_Load(object sender, EventArgs e)
        {
            label2.Text = bookName;
            label4.Text = auther;
        }

        //计算日期
        private void dateTimePicker1_ValueChanged(object sender, EventArgs e)
        {
            DateTime dateTime = DateTime.Parse(dateTimePicker1.Text);
            if (dateTime> DateTime.Now)
            {
                label8.Visible = true;
                label8.Text = ((dateTime - DateTime.Now).Days + 1).ToString();
                label10.Visible = true;
            }
            else
            {
                label8.Visible=false;
                label10.Visible = false;
            }
            
        }
    }
}
